package com.ruoyi.web.controller.danger;

import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.PersonCharge;
import com.ruoyi.system.service.IPersonChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *  安全包保责任落实
 */
@RestController
@RequestMapping("/charge")
public class PersonChargeController extends BaseController {

    @Autowired
    private OssTemplate template;

    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private IPersonChargeService personChargeService;

    @PostMapping("/upload")
    public AjaxResult createPersonCharge(MultipartFile file,String user) {

        try {
            template.putObject(ossProperties.getBucketName(), file.getOriginalFilename(), file.getInputStream(), file.getSize(), file.getContentType());
        } catch (Exception e) {
            return AjaxResult.error("500",e.getMessage());
        }

        return AjaxResult.success(file.getOriginalFilename());
    }

    @PostMapping("/create")
    public AjaxResult create(@RequestBody PersonCharge personCharge) {
        personChargeService.create(personCharge);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public TableDataInfo list(PersonCharge personCharge) {
        startPage();
        List<PersonCharge> personChargeList = personChargeService.list(personCharge);
        return getDataTable(personChargeList);
    }

    @GetMapping("/delete")
    public AjaxResult delete(Integer id) {
        personChargeService.delete(id);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody PersonCharge personCharge) {
        personChargeService.update(personCharge);
        return AjaxResult.success();
    }

    @GetMapping("/deleteFile")
    public AjaxResult deleteFile(Integer id) {
        personChargeService.deleteFile(id);
        return AjaxResult.success();
    }

}
